package com.black.common.core.domain.resp;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 登录成功后，存放用户信息的对象
 *
 * @author L
 */
@Getter
@Setter
public class AdminUserDetails implements UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间(令牌创建时间)
     */
    private Long loginTime;

    /**
     * 过期时间(令牌过期时间)
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 用户信息
     */
    private AdminUserResp user;

    /**
     * 构造方法,JSON序列化时使用
     */
    public AdminUserDetails() {
    }

    public AdminUserDetails(AdminUserResp user, Set<String> permissions) {
        this.user = user;
        this.permissions = permissions;
        this.userId = user.getUserId();
        this.deptId = user.getDept().getDeptId();
        this.ipaddr = user.getLoginIp();
        this.loginLocation = user.getLoginLocation();
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of();
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.user.getPassword();
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return this.user.getUserName();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
